#!/usr/bin/perl

require './accounts.pl';

&ReadParse();

my %user;
my $newpasswd = "";
my ($findflag, $uid, $error_msg, $error_title, @glist, $username, $fullname);
my ($logminor, $logpriority, $event, $formatstr);

$username = $in{"username"};
$fullname = $in{"fullname"};

unless($username)
{
	$error_msg = $text{'error_msg_no_username'};
}

unless($error_msg)
{
	$error_msg = &check_simbol_uname($username);
	if($error_msg)
	{
		$logminor= $text{'module_name'};
		$logpriority=$text{'naslog_warning'};
		$event = $text{'accounts_naslog_event_create_fail_uname_err_format'};
		$formatstr = sprintf $text{'accounts_naslog_create_fail_uname_err_format'}, $username;	
	}
}

unless($error_msg)
{
	$error_msg = &check_simbol_fullname($fullname);
	if($error_msg)
	{
		$logminor= $text{'module_name'};
		$logpriority=$text{'naslog_warning'};
		$event = $text{'accounts_naslog_event_create_fail_fullname_err_format'};
		$formatstr = sprintf $text{'accounts_naslog_create_fail_fullname_err_format'}, $fullname;
	}
}

unless($error_msg)
{
	$uid = &local_user_exist($username);
	if($uid >= 0)
	{
		$error_msg = $text{'error_msg_user_exist_local'};

		$logminor= $text{'module_name'};
		$logpriority=$text{'naslog_warning'};
		$event = $text{'accounts_naslog_event_create_fail_uname_exist_local'};
		$formatstr = sprintf $text{'accounts_naslog_create_fail_uname_exist_local'}, $username;
	}
}

unless($error_msg)
{
	$uid = &find_uid_by_name($username);
	if($uid < 0)
	{
		$error_msg = "";
		$user{"min"} = 0;
		$user{"shell"} = "/sbin/nologin";
		$user{"real"} = $in{'fullname'};
		$user{"gid"} = &get_users_groupid();
		$user{"inactive"} = "";
		$user{"user"}=$in{"username"};
		$user{"home"}="/sysvol/.mail/$in{'username'}";
		$user{"plainpass"}=$in{"passwd"};
		
		&foreign_require("setpass", "setpass-lib.pl");
		$newpasswd = &foreign_call("setpass", "crt_cryptpasswd", $in{'passwd'});
		
		$user{"pass"}=$newpasswd;
		$user{"max"}=9999;
		$user{"change"}=11915;
		$user{"warn"}="";
		$user{"expire"}="";
		$user{"uid"}= &get_new_userid();
		if($in{'disabled'})
		{
			$user{"disabled"} = "yes";
		}
		else
		{
			$user{"disabled"} = "no";
		}
		
		@glist = split(/\s+/, $in{'listinput'});
		$user{"glist"} = [@glist];
	}
	else
	{
		if($username)
		{
			$error_msg = $text{'error_msg_user_exist_sys'};
			$logminor= $text{'module_name'};
			$logpriority=$text{'naslog_warning'};
			$event = $text{'accounts_naslog_event_create_fail_uname_exist_sys'};
			$formatstr = sprintf $text{'accounts_naslog_create_fail_uname_exist_sys'}, $username;
		}
	}
}

if($error_msg)
{
	if($event)
	{
		&nas_syslog($logminor, $logpriority, $event, $formatstr);
	}
	&error_setup($text{'error_title_create_user'});
	&error($error_msg);	
}
else
{

	&create_user(\%user);
	if (-d $user{"home"}) {
		`chown -R $username:users $user{"home"}`;
	}
	&sync_user_with_quota($username);

	$logminor= $text{'module_name'};
	$logpriority=$text{'naslog_info'};
	$event = $text{'accounts_naslog_event_create_success'};
	$formatstr = sprintf $text{'accounts_naslog_create_success'}, $username;
	&nas_syslog($logminor, $logpriority, $event, $formatstr);

	&redirect("index.cgi");
}

